<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <template #header>
          <el-row type="flex" justify="end">
            <el-button type="primary" size="small" @click="addFn">添加角色</el-button>
          </el-row>
        </template>
        <!-- 表格 -->
        <el-table border :data="rolesData">
          <el-table-column label="角色名称" prop="title" />
          <el-table-column label="角色描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" size="small" @click="allotPerm(row.id)">分配权限</el-button>
              <el-button size="small" @click="editFn(row.id)">编辑</el-button>
              <el-button type="primary" size="danger" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center">
          <el-pagination
            background
            layout="prev, pager, next"
            :page-size="limit"
            :total="total"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 添加编辑弹窗 -->
      <RoleDialog ref="roles" :role-dialog.sync="roleDialog" @updateInfo="getRolesList" />
      <!-- 分配权限弹窗 -->
      <RolesPermissions ref="Permiss" :perm-dialog.sync="permDialog" />
    </div>
  </div>
</template>

<script>
import { getRolesInfo, getRoleCount, delRoleInfo } from '@/api/roles'

import RoleDialog from './components/roles-dialog.vue'
import RolesPermissions from './components/roles-permissions.vue'
export default {
  components: { RoleDialog, RolesPermissions },
  data() {
    return {
      limit: 5, // 每页的条数
      start: 0, // 开始获取的位置
      rolesData: [],
      total: 0,
      roleDialog: false,
      permDialog: false
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 分配权限
    allotPerm(id) {
      this.$refs.Permiss.getPermissions(id)
      this.permDialog = true
    },
    // 删除功能
    async delFn(id) {
      this.$confirm('是否确认删除?', '提示', {
        type: 'warning'
      }).then(async() => {
        await delRoleInfo(id)
        if (this.start !== 0 && this.rolesData.length === 1) {
          const start = parseInt(this.total / this.limit)
          this.currentChange(start)
        } else {
          this.getRolesList()
        }
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      }).catch((err) => {
        console.log('取消', err)
      })
    },
    // 编辑功能
    editFn(id) {
      this.roleDialog = true
      this.$refs.roles.getByIdInfo(id)
    },
    // 添加功能
    addFn() {
      this.roleDialog = true
    },
    // 分页功能
    currentChange(start) {
      this.start = 0 + (start - 1) * this.limit
      this.getRolesList()
    },
    // 获取数据
    async getRolesList() {
      const res = await getRolesInfo({ _limit: this.limit, _start: this.start })
      const sult = await getRoleCount()
      // console.log(res)
      // console.log(sult)
      this.total = sult.data
      this.rolesData = res.data
    }
  }
}
</script>

<style>

</style>
